package org.example.likou150.tree;

import java.util.ArrayList;

// https://leetcode.cn/problems/binary-search-tree-iterator/?envType=study-plan-v2&envId=top-interview-150
public class BSTIterator {
    ArrayList<Integer> arr = new ArrayList<>();
    private int index = 0;

    public BSTIterator(TreeNode root) {
        initArr(root);
        index = -1;
    }

    private void initArr(TreeNode root){
        if(root==null){
            return;
        }

        initArr(root.left);
        arr.add(root.val);
        initArr(root.right);
    }

    public int next() {
        return arr.get(++index);
    }

    public boolean hasNext() {
        return index < arr.size() - 1;
    }

    private class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
 }
}
